package com.hmall.trade.Listener;

import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class PayStatusListener {

    private final IOrderService orderService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "trade.pay.success.queue", durable = "true"),
            exchange = @Exchange(value = "pay.direct"), //默认类型为direct
            key = "pay.success"

    ))
    public void ListenPayStatus(Long orderId) {
        //解决业务幂等性问题的方案==>（方案二 结合业务逻辑进行实现）（方案一 设置消息唯一id进行判断）
        //1.查询订单
        Order order = orderService.getById(orderId);
        //2.判断订单状态是否为未支付
        if (order == null || order.getStatus() != 1) { //订单状态为已支付或关闭
            return; //订单状态异常直接跳过
        }
        //3.标记订单为已支付
        orderService.markOrderPaySuccess(orderId);

    }
}
